package com.xinsoft.controller.mes;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xinsoft.aop.ano.SysLog;
import com.xinsoft.common.BusinessType;
import com.xinsoft.common.Response;
import com.xinsoft.constant.Const;
import com.xinsoft.entity.dto.MesShiftParam;
import com.xinsoft.entity.po.MesShift;
import com.xinsoft.entity.po.SysConfig;
import com.xinsoft.service.MesShiftService;
import com.xinsoft.service.SysConfigService;
import com.xinsoft.utils.TokenUtil;
import com.xinsoft.validator.ValidatorUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;

/**
 * @ClassName MesShiftController
 * @Description 班制管理控制层
 * @Author Li9527
 * @Date 2023-7-17 15:43:16
 * @Version 1.0
 */
@RestController
@RequestMapping("/mes/shift")
@Api(value = "MesShiftController", tags = {"班制管理控制层"})
public class MesShiftController {

	@Autowired
	private MesShiftService mesShiftService;

	@Autowired
	private SysConfigService configService;
	/**
	 * @Author Li9527
	 * @Description 班制管理-新增
	 * @Date 2023/5/17 9:57
	 * @params   dept
	 * @return com.xinsoft.common.Response<?>
	 **/
	@SysLog(title = "班制管理-新增", businessType = BusinessType.INSERT)
	@ApiOperation("新增")
	@PostMapping("/add")		
	public Response<?> add(@RequestBody MesShift mesShift) {
		// 参数校验
		ValidatorUtils.validateEntity(mesShift);
		// 默认已审核
		mesShift.setAuditId(Const.ADMIN_UID);
		mesShift.setAuditStatus(Integer.valueOf(configService.getOne(new QueryWrapper<SysConfig>().eq("param_key","MES_SHIFT_DEFAULT_AUDIT_STATUS")).getParamValue()));
		MesShift oldmesShift = mesShiftService.getOne(new QueryWrapper<MesShift>()
				.eq("shift_name",mesShift.getShiftName())
				.or().eq("shift_code",mesShift.getShiftCode()));
		if(oldmesShift != null){
			return Response.fail("该班制名称或班制代码已存在,请不要重复添加");
		}
		return Response.succeed(mesShiftService.save(mesShift));
	}

	/**
	 * @Author Li9527
	 * @Description 更新
	 * @Date 2023/5/17 9:57
	 * @params   dept
	 * @return com.xinsoft.common.Response<?>
	 **/
	@ApiOperation("更新")
	@SysLog(title = "班制管理-编辑", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	public Response<?> edit(@RequestBody MesShift mesShift) {
		// 参数校验
		MesShift oldmesShift = mesShiftService.getOne(new QueryWrapper<MesShift>().eq("shift_name",mesShift.getShiftName()));
		if(oldmesShift != null && !mesShift.getId().equals(oldmesShift.getId())){
			return Response.fail("该班制已存在,请不要重复添加");
		}
		ValidatorUtils.validateEntity(mesShift);
		return Response.succeed(mesShiftService.updateById(mesShift));
	}

	/**
	 * @Author Li9527
	 * @Description 获取详情
	 * @Date 2023/5/17 9:57
	 * @params   id
	 * @return com.xinsoft.common.Response<?>
	 **/
	@ApiOperation("获取详情")
	@GetMapping("/detail")
	public Response<?> detail(@RequestParam(value = "id", defaultValue = "", required = true) Integer id) {
		return Response.succeed(mesShiftService.getById(id));
	}

	/**
	 * @Author Li9527
	 * @Description 删除
	 * @Date 2023/5/12 9:20
	 * @params   ids
	 * @return com.xinsoft.common.Response<?>
	 **/
	@ApiOperation("删除")
	@SysLog(title = "班制管理-删除", businessType = BusinessType.DELETE)
	@PostMapping("/del")
	public Response<?> del(@RequestBody List<Integer> ids) {
		for (int i = 0; i < ids.size(); i++) {
			MesShift mesShift = mesShiftService.getById(ids.get(i));
			if(mesShift != null && mesShift.getAuditStatus().equals(Const.AUDIT_STATUS_NORMAL)){
				return Response.fail("存在已审核班制，不允许删除");
			}
		}
		mesShiftService.removeByIds(ids);
		return Response.succeed();
	}

	/**
	 * @Author Li9527
	 * @Description 班制管理分页
	 * @Date 2023/5/18 10:51
	 * @params   param
	 * @return com.xinsoft.common.Response<?>
	 **/
	@PostMapping("/pageList")
	public Response<?> pageList(@Valid @RequestBody MesShiftParam param) {
		return Response.succeed(mesShiftService.findPageListByParam(param));
	}

	/**
	 * @Author Li9527
	 * @Description 批量审核
	 * @Date 2023/5/12 9:20
	 * @params   ids
	 * @return com.xinsoft.common.Response<?>
	 **/
	@ApiOperation("班制管理-批量审核")
	@SysLog(title = "班制管理-批量审核", businessType = BusinessType.UPDATE)
	@PostMapping("/auditBatch")
	public Response<?> auditBacth(@RequestBody List<Integer> ids) {
		Integer auditId =  TokenUtil.getUser().getId();
		List<MesShift> list = mesShiftService.list(new QueryWrapper<MesShift>().in("id",ids));
		for (int i = 0; i < list.size(); i++) {
			list.get(i).setAuditStatus(Const.AUDIT_STATUS_NORMAL);
			list.get(i).setAuditId(auditId);
		}
		mesShiftService.updateBatchById(list);
		return Response.succeed();
	}

	/**
	 * @Author Li9527
	 * @Description 批量反审核
	 * @Date 2023/5/12 9:20
	 * @params   ids
	 * @return com.xinsoft.common.Response<?>
	 **/
	@ApiOperation("班制管理-批量反审核")
	@SysLog(title = "班制管理-批量反审核", businessType = BusinessType.UPDATE)
	@PostMapping("/unAuditBatch")
	public Response<?> unAuditBatch(@RequestBody List<Integer> ids) {
		List<MesShift> list = mesShiftService.list(new QueryWrapper<MesShift>().in("id",ids));
		Integer auditId =  TokenUtil.getUser().getId();
		for (int i = 0; i < list.size(); i++) {
			list.get(i).setAuditStatus(Const.AUDIT_STATUS_WAIT);
			list.get(i).setAuditId(auditId);
		}
		mesShiftService.updateBatchById(list);
		return Response.succeed();
	}

}
